package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AdminMenus;
import com.dingreading.cloud.admin.entity.SubjectsFiles;
import com.dingreading.cloud.admin.service.SubjectsFilesService;
import com.dingreading.cloud.common.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RefreshScope
@RestController
@RequestMapping("/system/subjectsFiles")
@Api(tags = "总部-备课系统文件管理")
public class SubjectsFilesController {

    @Resource
    private SubjectsFilesService subjectsFilesService;

    @ApiOperation(value = "根据备课分类ID获取所有文件")
    @PostMapping(value = "/getBySubjectId", produces = "application/json; charset=UTF-8")
    public R<Object> getBySubjectId(
            HttpServletRequest request,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "备课子分类id") @RequestParam(value = "subjectId") Long subjectId
    ) {
        return subjectsFilesService.getBySubjectId(request, headStaffUid, headStaffName, subjectId);
    }

    @ApiOperation(value = "上传/修改备课文件")
    @PostMapping(value = "/uploadFile")
    public R<Object> uploadFile(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "备课子分类id") @RequestParam(value = "subjectId") Long subjectId,
            @ApiParam(value = "文件名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "文件地址") @RequestParam(value = "fileUrl") String fileUrl,
            @ApiParam(value = "文件类型 0.助教文案 1.阅读指南 2.资料下载 3.新书预告 4.入学测试 5.教研视频") @RequestParam(value = "fileType") Integer fileType,
            @ApiParam(value = "文件id") @RequestParam(value = "id", required = false) Long id
    ) {
        return subjectsFilesService.uploadFile(headStaffUid, headStaffName, subjectId, name, fileUrl, fileType, id);
    }

    @ApiOperation(value = "根据备课分类ID获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "文件id") @RequestParam(value = "id") Long id
    ) {
        return subjectsFilesService.getBean(headStaffUid, headStaffName, id);
    }

    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "文件id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        return subjectsFilesService.updateEnabled(headStaffUid, headStaffName, id, enabled);
    }


    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "学科文件id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        SubjectsFiles data = subjectsFilesService.getById(id);
        if (data == null)
            return R.failGet();

        if (type.equals(1)) {
            // 上移
            SubjectsFiles preData = subjectsFilesService.getLtShowOrder(data.getSubjectsId(), data.getShowOrder());
            if (preData == null) return R.fail("已在最前");
            // 和前一条记录的序号互换
            Integer prevOrderNum = preData.getShowOrder();
            Integer curOrderNum = data.getShowOrder();
            // 序号互换
            subjectsFilesService.updateShowOrder(preData.getId(), curOrderNum);
            subjectsFilesService.updateShowOrder(data.getId(), prevOrderNum);
        } else if (type.equals(2)) {
            // 下移
            SubjectsFiles nextData = subjectsFilesService.getGtShowOrder(data.getSubjectsId(), data.getShowOrder());
            if (nextData == null) return R.fail("已在最后");
            // 和后一条记录的序号互换
            Integer nextOrderNum = nextData.getShowOrder();
            Integer curOrderNum = data.getShowOrder();
            // 序号互换
            subjectsFilesService.updateShowOrder(nextData.getId(), curOrderNum);
            subjectsFilesService.updateShowOrder(data.getId(), nextOrderNum);
        } else {
            return R.fail("类型错误");
        }
        return R.ok();
    }


    @ApiOperation(value = "学科层级的教研视频迁移到学科文件表")
    @PostMapping(value = "/videoCopySubjectFile")
    public R<Object> videoCopySubjectFile() {
        return subjectsFilesService.videoCopySubjectFile();
    }


    @ApiOperation(value = "学科的文件设置查看类型")
    @PostMapping(value = "/subjectFileSetViewType")
    public R<Object> subjectFileSetViewType() {
        return subjectsFilesService.subjectFileSetViewType();
    }

    @ApiOperation(value = "设置文件的序号")
    @PostMapping(value = "/setShowOrder")
    public R<Object> setShowOrder() {
        return subjectsFilesService.setShowOrder();
    }

}
